package org.zp.handler;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zp.dto.Paging;
import org.zp.dto.SelectadminlogDto;
import org.zp.pojo.AdminInfo;
import org.zp.pojo.AdminLogInfo;
import org.zp.pojo.UserInfo;
import org.zp.service.AdminInfoService;
import org.zp.service.AdminLogInfoService;
import org.zp.util.Ajax;

import com.github.pagehelper.PageInfo;
/**
 * 日志业务
 * @FileName LogHandler.java
 * @author zp
 * @package 2020年8月12日
 * @TODO
 */
@RestController
public class LogHandler {
@Resource
private AdminInfoService a;
@Resource
private AdminLogInfoService log;
/**
 * 获得所有管理员的对象
 * @param req
 * @param resp
 */
@GetMapping("getadminName.act")
private void getadminName(HttpServletRequest req, HttpServletResponse resp) {
	List<AdminInfo> list = a.showAdmin();
	Ajax.success(resp, "list", list);
}
/**
 * 日志多条件查询
 * @param dto
 * @param req
 * @param resp
 */
@GetMapping("selectadminlog.act")
private void selectadminlog(SelectadminlogDto dto,HttpServletRequest req, HttpServletResponse resp) {
	Integer startpage = Integer.parseInt(dto.getStartpage());
	Integer limitpage = Integer.parseInt(dto.getLimitpage());
	Long aid = (long) Integer.parseInt(dto.getAid());
	String start = dto.getStart();
	String end = dto.getEnd();
	if (aid==0) {
		aid = null;
	}
	if (start.equals("")) {
		start = null;
	}
	if (end.equals("")) {
		end = null;
	}
	if (start!=null&&end!=null) {
	DateFormat df = new SimpleDateFormat("yyyy-MM-dd");//日期格式处理
    try {
			Date star = df.parse(start);
			Date endd = df.parse(end);
			if (star.getTime()>endd.getTime()){//比较时间的大小
				Ajax.error(resp,"时间有误",10001);
				return;
        }
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	List<AdminLogInfo> list = log.selectlog(startpage,limitpage,aid,start,end);
	PageInfo<AdminLogInfo>pageInfo = new PageInfo<AdminLogInfo>(list);
	Integer count = (int) pageInfo.getTotal();
	System.out.println("用户多条件查询count:"+count);
	Ajax.success(resp, "list","count", list,count);
}
/**
 * 日志删除
 * @param lid
 * @param req
 * @param resp
 */
@PostMapping("LogDel.act")
private void LogDel(String lid,HttpServletRequest req, HttpServletResponse resp) {
	int Lid = 0;
	try {
		Lid = Integer.parseInt(lid);
	} catch (NumberFormatException e) {
		Ajax.error(resp, "日志id不正确", 10000);
	}
	if (Lid <= 0) {
		Ajax.error(resp, "日志id不正确", 10000);
		return;
	}
	Integer index = log.LogDel(Lid);
	if (index>0) {
		Ajax.success(resp);
		System.out.println("删除成功");
	}else {
		Ajax.error(resp, "删除失败", 10001);
		System.out.println("删除失败");
	}
}
}
